<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of statistic
 *
 * @author Administrator
 */
class statistic extends CI_Controller {

  //put your code here

  public function __construct() {
    parent::__construct();
    if (!userdata('provider_id')) {
      redirect('provider');
    }
  }

  private function _process_data() {
    $promotions = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id'),
        'is_active' => TRUE, 'status' => PROMOTION_PUBLISH), null, null, null, null, null, null, null, null, null, null, null, array('date_publish' => 'desc'));
    $dates = array();
    if (!empty($_POST['from']) && !empty($_POST['to'])) {
      $search_min = strtotime($_POST['from']);
      $search_max = strtotime($_POST['to']);
      foreach ($promotions as $i => $promotion) {
        $time = max($search_min, $promotion['start_date']);
        $promotions[$i]['temp_plot'] = array();
        $end_date = min($search_max, $promotion['end_date']);
        while ($time <= $end_date) {
          $date = date('d/m/Y', $time);
          $num_download = $this->promotion_download_model->count(array('promotion_id' => $promotion['id'],
              'date_create >= ' => $time, 'date_create  <= ' => min($end_date, $time + 86400)));
          $promotions[$i]['temp_plot'][$date] = $num_download;
          if (!in_array($date, $dates)) {
            $dates[$time] = $date;
          }
          $time += 86400;
        }
      }
      $min = $search_min;
      $max = min($search_max, time());
    } else {
      $min = time();
      $max = 0;
      foreach ($promotions as $i => $promotion) {
        $time = $promotion['start_date'];
        $min = $time < $min ? $time : $min;
        $max = $promotion['end_date'] > $max ? $promotion['end_date'] : $max;
        $promotions[$i]['temp_plot'] = array();
        $end_date = min(time(), $promotion['end_date']);
        while ($time <= $end_date) {
          $date = date('d/m/Y', $time);
          $num_download = $this->promotion_download_model->count(array('promotion_id' => $promotion['id'],
              'date_create >= ' => $time, 'date_create  <= ' => min($end_date, $time + 86400)));
          $promotions[$i]['temp_plot'][$date] = $num_download;
          if (!in_array($date, $dates)) {
            $dates[$time] = $date;
          }
          $time += 86400;
        }
      }
      $time = $min;
      $max = min($max, time());
    }
    while ($time <= $max) {
      $date = date('d/m/Y', $time);
      if (!in_array($date, $dates)) {
        $dates[$time] = $date;
      }
      $time += 86400;
    }
    ksort($dates);
    return array('promotions' => $promotions, 'dates' => $dates, 'max' => $max, 'min' => $min);
  }

  public function index() {
    $temp = $this->_process_data();
    $promotions = $temp['promotions'];
    $dates = $temp['dates'];
    $max = $temp['max'];
    $min = $temp['min'];
    unset($temp);
    foreach ($promotions as $i => $promotion) {
      $promotions[$i]['plot'] = array();
      $j = 0;
      foreach ($dates as $date) {
        $promotions[$i]['plot'][$j] = isset($promotion['temp_plot'][$date]) ? $promotion['temp_plot'][$date] : null;
        $j++;
      }
      unset($promotions[$i]['temp_plot']);
      $promotions[$i]['plot'] = $this->trim_plot($promotions[$i]['plot']);
    }
    $ticks = '';
    $i = 0;
    foreach ($dates as $date) {
      $ticks.= '[' . ($i) . ', "' . $date . '"]';
      if ($i < count($dates) - 1)
        $ticks.= ','; $i++;
    }
    $datasets = '{';
    $num_promotion = count($promotions);
    foreach ($promotions as $k => $promotion) {
      $datasets.='"' . $promotion['id'] . '": {
                label: "' . $promotion['title'] . '", data: [';
      foreach ($promotion['plot'] as $i => $plot) {
        if ($plot === null)
          $datasets.= 'null';
        else
          $datasets.= '[' . ($i) . ',' . $plot . ']';
        $datasets.= ($i < count($promotion['plot']) - 1 ? ',' : '');
      }
      $datasets.= ']}';
      $datasets.= ($k < $num_promotion - 1 ? ',' : '');
    }
    $datasets .= '}';
    $this->load->view('provider/statistic', array(
        'max' => $max, 'min' => $min,
        'dates' => '"' . implode('","', $dates) . '"', 'ticks' => $ticks,
        'promotions' => $promotions, 'datasets' => $datasets,
        'title_html' => $this->lang->line('label.provider.title.statistic')));
  }

  public function excel() {
    $temp = $this->_process_data();
    $promotions = $temp['promotions'];
    $dates = $temp['dates'];
    unset($temp);
    $this->load->library('excel_xml');
    $data = array(array_merge(array(''), $dates));
    foreach ($promotions as $i => $promotion) {
      $promotions[$i]['plot'] = array();
      $j = 0;
      foreach ($dates as $date) {
        $promotions[$i]['plot'][$j] = isset($promotion['temp_plot'][$date]) ? $promotion['temp_plot'][$date] : '0';
        $j++;
      }
      unset($promotions[$i]['temp_plot']);
      $data[] = array_merge(array($promotion['title']), $this->trim_plot($promotions[$i]['plot']));
      unset($promotion);
    }
    $from = str_replace('/', '-', reset($dates));
    $to = str_replace('/', '-', end($dates));
    $xls = new excel_xml('UTF-8', TRUE, 'export from ' . $from . ' to ' . $to);
    $xls->addArray($data);
    $xls->generateXML('export_' . time());
    exit;
  }

  private function trim_plot($plot) {
    $index = 0;
    $n = count($plot);
    for ($i = 0; $i < $n; $i++) {
      if ($plot[$i] != 0) {
        $index = $i;
        break;
      }
    }
    for ($i = 0; $i < $index - 1; $i++) {
      $plot[$i] = null;
    }

    $index = $n - 1;
    for ($i = $n - 1; $i >= 0; $i--) {
      if ($plot[$i] != 0) {
        $index = $i;
        break;
      }
    }
    for ($i = $n - 1; $i > $index + 1; $i--) {
      $plot[$i] = null;
    }
    return $plot;
  }

}